package com.pxst.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.pxst.bank.entity.SysBankServiceTimeEntity;
import com.pxst.bank.mapper.SysServiceTimeMapper;
import com.pxst.bank.req.ServiceTimeAddReq;
import com.pxst.bank.req.ServiceTimeReq;
import com.pxst.bank.resp.SysBankServiceTimeResp;
import com.pxst.bank.service.SysBankServiceTimeService;
import com.pxst.resp.ApiResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * sys_bank_service_time
 */
@Service
@Slf4j
public class SysBankServiceTimeServiceImpl extends ServiceImpl<SysServiceTimeMapper, SysBankServiceTimeEntity> implements SysBankServiceTimeService {

    @Resource
    private SysServiceTimeMapper serviceTimeMapper;
    /**
     * 暂停时间设定-列表（条件查询） sys_bank_service_time,sys_available_bank 联表查询
     * @param serviceTimeReq 查询请求类
     * @return
     */
    @Override
    public ApiResp<List<SysBankServiceTimeResp>> getServiceTimeByCondition(ServiceTimeReq serviceTimeReq) {
        // 1.分页
        PageHelper.startPage(serviceTimeReq.getPage(),serviceTimeReq.getRp());
        // 2. 查询 sys_bank_service_time 和 sys_available_bank 中相关的字段
        List<SysBankServiceTimeResp> sysBankServiceTimeRespList = serviceTimeMapper.selectJoin(serviceTimeReq);
        log.info("获取sys_bank_service_time的信息为:{}",sysBankServiceTimeRespList);
        return ApiResp.sucess(sysBankServiceTimeRespList);
    }

    /**
     * 修改状态
     * @param id
     * @return
     */
    @Override
    public ApiResp<String> updateStatusById(Long id, Integer status) {
        LambdaUpdateWrapper<SysBankServiceTimeEntity> lqw = new LambdaUpdateWrapper<>();
        // 修改指定id的对象的status
        lqw.set(SysBankServiceTimeEntity::getStatus,status)
                .eq(SysBankServiceTimeEntity::getId,id);
        serviceTimeMapper.update(serviceTimeMapper.selectById(id),lqw);

        return ApiResp.sucess();
    }

    /**
     * 暂停时间表设定-新增
     * @param serviceTimeAddReq
     * @return
     */
    @Override
    public ApiResp<SysBankServiceTimeEntity> saveServiceTime(ServiceTimeAddReq serviceTimeAddReq) {
        if(serviceTimeAddReq.getStartDatetime() == null || serviceTimeAddReq.getEndDatetime() == null) {
            return ApiResp.paramError("时间输入不完整");
        }
        if(serviceTimeAddReq.getType() == 1 && StringUtils.isBlank(serviceTimeAddReq.getMessage())){
            return ApiResp.paramError("信息输入不完整");
        }
        if(serviceTimeAddReq.getType() != 1 && serviceTimeAddReq.getFrequency() == null){
            return ApiResp.paramError("频率输入不完整");
        }
        SysBankServiceTimeEntity isExist = serviceTimeMapper.selectOne(
                new LambdaQueryWrapper<SysBankServiceTimeEntity>().eq(SysBankServiceTimeEntity::getAvailableBankId, serviceTimeAddReq.getAvailableBankId()));
        if(isExist != null) {
            return ApiResp.bussError("可用银行id不能重复");
        }
        if (serviceTimeMapper.count() >= 30) {
            return ApiResp.bussError("最多新增30笔");
        }
        SysBankServiceTimeEntity sysBankServiceTimeEntity = new SysBankServiceTimeEntity();
        BeanUtils.copyProperties(serviceTimeAddReq,sysBankServiceTimeEntity);
        serviceTimeMapper.insert(sysBankServiceTimeEntity);
        // log.info("返回的实体类的id:{}",sysBankServiceTimeEntity.getId());
        // 插入一条信息并返回对象
        return ApiResp.sucess(serviceTimeMapper.selectById(sysBankServiceTimeEntity.getId()));
    }


    /**
     *
     * @param id
     * @return
     */
    @Override
    public ApiResp<String> deleteById(Long id) {
        // 根据id直接删除对应信息
        serviceTimeMapper.deleteById(id);
        return ApiResp.sucess();
    }
}
